package org.bite.lesson04;

/**
 * 使用单独的锁对象
 *
 * @Author kiku-wa
 */
public class Demo405 {
    public static void main(String[] args) throws InterruptedException{
        Counter405 counter405 = new Counter405();
        Thread t1 = new Thread(()->{
            for(int i = 0; i < 50000; i++){
                counter405.increase();
            }
        });
        Thread t2 = new Thread(()->{
            for(int i = 0; i < 50000; i++){
                counter405.increase();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("count = " + Counter405.count);
    }
}
class Counter405 {
    public static int count = 0;
    Object locker = new Object();
    public void increase() {
        synchronized(locker){
            count++;
        }
    }
}
